﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HttpRequest
{
    public partial class PointDistanceForm : Form
    {
        public PointDistanceForm()
        {
            InitializeComponent();
        }

        #region 公里 换 米

        /// <summary>
        /// 公里 换 米
        /// 计算坐标之间的距离
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndistance_Click(object sender, EventArgs e)
        {
            var lat1 = Convert.ToDouble(txtlat1.Text.Trim());
            var lon1 = Convert.ToDouble(txtlon1.Text.Trim());
            var lat2 = Convert.ToDouble(txtlat2.Text.Trim());
            var lon2 = Convert.ToDouble(txtlon2.Text.Trim());
            var distance = CalculateDistance(lat1, lon1, lat2, lon2);
            txtResult.Text += $"The distance between the two points is: {distance*100} 公里(km)  {distance * 100*1000} 米  {Environment.NewLine}";
        }
        public static double CalculateDistance(double x1, double y1, double x2, double y2)
        {
            // 使用Math.Pow计算每个坐标的差的平方
            double deltaX = Math.Pow(x2 - x1, 2);
            double deltaY = Math.Pow(y2 - y1, 2);

            // 使用Math.Sqrt计算平方和的平方根，得到距离
            return Math.Sqrt(deltaX + deltaY);
        }

        #endregion

        #region 方法2英里 换 米

        private const double EarthRadius = 6371000; // 地球半径，单位米

        public static double CalculateDistance1(double lat1, double lon1, double lat2, double lon2)
        {
            var deltaLat = ToRadians((lat2 - lat1));
            var deltaLon = ToRadians((lon2 - lon2));

            lat1 = ToRadians(lat1);
            lat2 = ToRadians(lat2);

            var a = Math.Sin(deltaLat / 2) * Math.Sin(deltaLat / 2) +
                    Math.Cos(lat1) * Math.Cos(lat2) *
                    Math.Sin(deltaLon / 2) * Math.Sin(deltaLon / 2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            var distance = EarthRadius * c;

            return distance;
        }

        private static double ToRadians(double angle)
        {
            return angle * (Math.PI / 180);
        }

        /// <summary>
        /// 英里 换 米
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndistance2_Click(object sender, EventArgs e)
        {
            var lat1 = Convert.ToDouble(txtlat1.Text.Trim());
            var lon1 = Convert.ToDouble(txtlon1.Text.Trim());
            var lat2 = Convert.ToDouble(txtlat2.Text.Trim());
            var lon2 = Convert.ToDouble(txtlon2.Text.Trim());
            var distance = CalculateDistance1(lat1, lon1, lat2, lon2);
            txtResult.Text += $"The distance2 between the two points is: {distance/1000} 英里  {distance / 1000 * 1609} 米{Environment.NewLine}";
        }

        #endregion
    }
}
